package domain.entities;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;

import java.util.ArrayList;
import java.util.List;

import static javax.persistence.GenerationType.IDENTITY;

/**
 * Vendorcentral entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "vendorcentral", catalog = "bms")
public class Vendorcentral implements java.io.Serializable {

	// Fields
    public static final String LOCAL_CATEGORY = "Local";
    public static final String CENTRAL_CATEGORY = "Central";

	private Integer id;
	private Address address;
	private String name;
    private String eclipseId;
    private VendorType type;
    private String activity;
//    private ProtocoloCompromisopais protocolo;
    private ContactPerson contactPerson;
    private ContactPerson secondaryContactPerson;
    private String externalValidationType;
    private String category;
    private List<NormalValues> normalValues = new ArrayList<NormalValues>();
    private List<ExternalValidation> externalValidations = new ArrayList<ExternalValidation>();
    private List<ProtocoloCompromisopais> protocolos = new ArrayList<ProtocoloCompromisopais>();
    private List<Centro> sites = new ArrayList<Centro>();

    // Constructors

	/** default constructor */
	public Vendorcentral() {
	}

	/** minimal constructor */
	public Vendorcentral(Address address
//            ,ProtocoloCompromisopais protocolo
    ) {
		this.address = address;
//		this.protocolo = protocolo;
	}

	/** full constructor */
	public Vendorcentral(Address address
//            ,ProtocoloCompromisopais protocolo
			,String name) {
		this.address = address;
//		this.protocolo = protocolo;
		this.name = name;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name = "ADDRESSid", nullable = false)
	public Address getAddress() {
		return this.address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Column(name = "name")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

    @Column(name = "eclipseId")
    public String getEclipseId() {
        return this.eclipseId;
    }

    public void setEclipseId(String eclipseId) {
        this.eclipseId = eclipseId;
    }

    @Column(name = "activity")
    public String getActivity() {
        return activity;
    }

    public void setActivity(String activity) {
        this.activity = activity;
    }

//    @ManyToOne(fetch = FetchType.EAGER)
//    public ProtocoloCompromisopais getProtocolo() {
//        return protocolo;
//    }
//
//    public void setProtocolo(ProtocoloCompromisopais protocolo) {
//        this.protocolo = protocolo;
//    }

    @ManyToOne(cascade = CascadeType.ALL)
    public VendorType getType() {
        return type;
    }

    public void setType(VendorType type) {
        this.type = type;
    }

    @OneToOne(cascade = CascadeType.ALL)
    public ContactPerson getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(ContactPerson contactPerson) {
        this.contactPerson = contactPerson;
    }

    @OneToOne(cascade = CascadeType.ALL)
    public ContactPerson getSecondaryContactPerson() {
        return secondaryContactPerson;
    }

    public void setSecondaryContactPerson(ContactPerson secondaryContactPerson) {
        this.secondaryContactPerson = secondaryContactPerson;
    }


    @Column(name = "externalValidationType")
    public String getExternalValidationType() {
        return this.externalValidationType;
    }

    public void setExternalValidationType(String externalValidationType) {
        this.externalValidationType = externalValidationType;
    }

    @Column(name = "category")
    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category){
        this.category = category;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "vendorcentral")
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<NormalValues> getNormalValues() {
        return this.normalValues;
    }

    public void setNormalValues(List<NormalValues> normalValues) {
        this.normalValues = normalValues;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "vendorcentral")
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<ExternalValidation> getExternalValidations() {
        return this.externalValidations;
    }

    public void setExternalValidations(List<ExternalValidation> externalValidations) {
        this.externalValidations = externalValidations;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name = "vendorCentral_site", catalog = "bms",
            joinColumns = {
                    @JoinColumn(name = "vendorId", nullable = false, updatable = false)
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "siteId", nullable = false, updatable = false, referencedColumnName = "id"),
            }
    )
    public List<Centro> getSites() {
        return sites;
    }

    public void setSites(List<Centro> sites) {
        this.sites = sites;
    }

    @ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinTable(name = "vendorCentral_protocoloCompromisoPais", catalog = "bms",
            joinColumns = {
                    @JoinColumn(name = "vendorId", nullable = false, updatable = false)
                        },
            inverseJoinColumns = {
                    @JoinColumn(name = "protocoloid", nullable = false, updatable = false, referencedColumnName = "protocoloid"),
                    @JoinColumn(name = "compromisopaisid", nullable = false, updatable = false, referencedColumnName = "compromisopaisid")
                        }
    )
    public List<ProtocoloCompromisopais> getProtocolos() {
        return protocolos;
    }

    public void setProtocolos(List<ProtocoloCompromisopais> protocolos) {
        this.protocolos = protocolos;
    }

    @Transient
    public String getCountriesForVendor(){
        String output= "";
        if(protocolos.size()==0){
            return output;
        }else if(protocolos.size()==1){
            return protocolos.get(0).getCompromisopais().getCountry();
        }else{
            int i=0;
            for( ; i< protocolos.size()-1; i++){
                output += protocolos.get(i).getCompromisopais().getCountry()+", ";
            }
            output += protocolos.get(i).getCompromisopais().getCountry();
            return output;
        }
    }

    @Transient
    public String getSitesForVendor(){
        String output= "";
        if(sites.size()==0){
            return output;
        }else if(sites.size()==1){
            return sites.get(0).getInstitucion();
        }else{
            int i=0;
            for( ; i< sites.size()-1; i++){
                output += sites.get(i).getInstitucion()+", ";
            }
            output += sites.get(i).getInstitucion();
            return output;
        }
    }

}